﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AO.Core;

namespace ZoneEngine.Functions
{
    class Function_hit : FunctionPrototype
    {
        public new int FunctionNumber = 53002;
        
        public new string FunctionName = "hit";
        
        public override int ReturnNumber()
        { 
            return FunctionNumber; 
        }
        
        public override bool Execute(Dynel Self, Dynel Caller, object Target, object[] Arguments)
        {
            lock (Self)
            {
                lock (Caller)
                {
                    lock (Target)
                    {
                        return FunctionExecute(Self, Caller, Target, Arguments);
                    }
                }
            }
        }
        
        public override string ReturnName()
        {
            return FunctionName;
        }

        public bool FunctionExecute(Dynel Self, Dynel Caller, object Target, object[] Arguments)
        {
            int Statnumber;
            int minhit;
            int maxhit;
            int dunno;
            if (Target is Statels.Statel)
            {
                Statnumber = Int32.Parse((string)Arguments[0]);
                minhit = Int32.Parse((string)Arguments[1]);
                maxhit = Int32.Parse((string)Arguments[2]);
                if (minhit > maxhit)
                {
                    minhit = maxhit;
                    maxhit = Int32.Parse((string)Arguments[1]);
                }
            }
            else
            {
                Statnumber = (int)Arguments[0];
                minhit = (int)Arguments[1];
                maxhit = (int)Arguments[2];
                if (minhit > maxhit)
                {
                    minhit = maxhit;
                    maxhit = (int)Arguments[1];
                }
            }
            Random rnd = new Random();
            ((Character)Self).Stats.Set(Statnumber, (uint)(((Character)Self).Stats.Get(Statnumber) + rnd.Next(minhit, maxhit)));
            return true; 
        }
    }
}
